package com.jjlin.chapter_2;

import java.util.*;
public class CD108{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    public static Node createNodeList(int[] nums){
        if(nums == null || nums.length < 1)
            return null;
        Node head = new Node(nums[0]);
        Node pre = head;
        for(int i = 1; i < nums.length; i++){
            Node cur = new Node(nums[i]);
            pre.next = cur;
            pre = cur;
        }
        return head;
    }

    public static void printNodeList(Node head){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static Node reversePart(Node head, int from, int to){
        int index = 0;
        Node fpre = null;//记录第from - 1个节点
        Node tpos = null;//记录第to + 1个节点
        Node node1 = head;
        while(node1 != null){
            index++;
            fpre = index == from - 1 ? node1 : fpre;
            tpos = index == to + 1 ? node1 : tpos;
            node1 = node1.next;
        }
        if(from > to || from < 1 || to > index)
            return head;
        node1 = fpre == null ? head : fpre.next;//记录第from个节点
        Node node2 = node1.next;
        node1.next = tpos;//连接第to + 1个节点
        Node next = null;
        while(node2 != tpos){//反转第from到to个节点
            next = node2.next;
            node2.next = node1;
            node1 = node2;
            node2 = next;
        }
        if(fpre != null){//第from个节点不是头节点
            fpre.next = node1;
            return head;
        }
        return node1;//第from个节点是头节点
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++)
            nums[i] = sc.nextInt();
        int L = sc.nextInt();
        int R = sc.nextInt();
        Node head = createNodeList(nums);
        head = reversePart(head, L, R);
        printNodeList(head);
    }

}

//自己最初的写法,当做记录
/**
 *     public static Node reversePart(Node head, int from, int to){
 *         if(head == null || from > to || from < 0 || to < 0)
 *             return head;
 *         Node newhead = new Node(-1);
 *         newhead.next = head;
 *         Node pre = newhead;
 *         Node cur = newhead;
 *         while(from-- > 0 && to-- > 0 && cur != null){
 *             pre = cur;
 *             cur = cur.next;
 *         }
 *         if(cur == null)
 *             return head;
 *         pre.next = null;
 *         Node list1 = pre;
 *         Node list2 = cur;
 *         while(to-- > 0 && cur != null){
 *             pre = cur;
 *             cur = cur.next;
 *         }
 *         if(cur == null)
 *             return head;
 *         Node list3 = cur.next;
 *         pre = list2;
 *         cur = pre.next;
 *         while(cur != list3){
 *             Node tmp = cur.next;
 *             cur.next = pre;
 *             pre = cur;
 *             cur = tmp;
 *         }
 *         list1.next = pre;
 *         list2.next = list3;
 *         return newhead.next;
 *     }
 * }
 */
